//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.royal.lr.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeUtil {
    public TimeUtil() {
    }

    public static String date(String fmt) {
        return (new SimpleDateFormat(fmt)).format(new Date());
    }

    public static String date(String fmt, long t) {
        return (new SimpleDateFormat(fmt)).format(new Date(t));
    }

    public static String date(String fmt, Date date) {
        return (new SimpleDateFormat(fmt)).format(date);
    }

    public static String date8() {
        return (new SimpleDateFormat("yyyyMMdd")).format(new Date());
    }

    public static String date10() {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }

    public static String date18() {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    }
    public static String date8(Date date) {
        return (new SimpleDateFormat("yyyyMMdd")).format(date);
    }
    public static String dateUTC() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        return sdf.format(new Date());
    }

    public static String dateUTCSecond() {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String dateUTCTo18(String date) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sdf.parse(date));
    }
    public static String dateUTCSecond(Date date) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        return sdf.format(date);
    }

    public static String dateUTC(Date date) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        return sdf.format(date);
    }
    public static String date10(Date date) {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }
    public static String date2HHmmss(String date) {
        return (new SimpleDateFormat("HH:mm:ss")).format(date);
    }
    public static Date strToDate(String date,String fmt) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(fmt);
            format.setLenient(false);
            return format.parse(date);
        } catch (ParseException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static long toMilliSec(String time14) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(time14.substring(0, 4)), Integer.parseInt(time14.substring(4, 6)) - 1, Integer.parseInt(time14.substring(6, 8)), Integer.parseInt(time14.substring(8, 10)), Integer.parseInt(time14.substring(10, 12)), Integer.parseInt(time14.substring(12, 14)));
        return cal.getTimeInMillis();
    }

    public static int getActualMaximum(String day8, int field) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(day8.substring(0, 4)), Integer.parseInt(day8.substring(4, 6)) - 1, Integer.parseInt(day8.substring(6, 8)));
        return cal.getActualMaximum(field);
    }

    public static int getActualMinimum(String day8, int field) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(day8.substring(0, 4)), Integer.parseInt(day8.substring(4, 6)) - 1, Integer.parseInt(day8.substring(6, 8)));
        return cal.getActualMinimum(field);
    }

    public static int getWeekIndex() {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(2);
        return c.get(3);
    }

    public static Date addMin(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(12, amount);
        return cal.getTime();
    }

    public static Date addHour(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(10, amount);
        return cal.getTime();
    }

    public static String addUTCHour(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(10, amount);
        return dateUTCSecond(cal.getTime());
    }

    public static Date addSec(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(13, amount);
        return cal.getTime();
    }

    public static String addDay(Date now, int amount,String fmt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(5, amount);

        return (new SimpleDateFormat(fmt)).format(cal.getTime());
    }

    public static String addDayOfYear(String now, int amount) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=sdf.parse(now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, amount);

        return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
    }

    public static String getAmountDayOfYear(int amount) throws ParseException {
        Date date=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, amount);
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(cal.getTime());
    }

    public static String getLastMonthLastDay() throws ParseException {
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        return (new SimpleDateFormat("yyyy-MM-dd")).format(cale.getTime());
    }

    public static String getLastMonthFirstDay() throws ParseException {
        Calendar   cal_1=Calendar.getInstance();//获取当前日期
         cal_1.add(Calendar.MONTH, -1);
         cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return (new SimpleDateFormat("yyyy-MM-dd")).format(cal_1.getTime());
    }


    public static String getMonthFirstDay() throws ParseException {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
         c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        return (new SimpleDateFormat("yyyy-MM-dd")).format(c.getTime());
    }

    public static String addStrDay(String now)throws Exception {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=sdf.parse(now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, 1);

        return sdf.format(cal.getTime());
    }

    public static Date addMonth(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(2, amount);
        return cal.getTime();
    }

    public static Date addYear(Date now, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(1, amount);
        return cal.getTime();
    }

    public static Date parseDate8(String date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            format.setLenient(false);
            return format.parse(date);
        } catch (ParseException var2) {
            return null;
        }
    }

    public static Date parseDate10(String date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            format.setLenient(false);
            return format.parse(date);
        } catch (ParseException var2) {
            return null;
        }
    }

    public static String parseString10(String date) {
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            format.setLenient(false);
            return format1.format(format.parse(date));
        } catch (ParseException var2) {
            return null;
        }
    }

    public static String parseDate17String10(String date) {
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            format.setLenient(false);
            return format1.format(format.parse(date));
        } catch (ParseException var2) {
            return null;
        }
    }
    public static boolean validateDate8(String date) {
        Date d = parseDate8(date);
        return d != null && date8(d).equals(date);
    }

    public static Date parseDatetime14(String datetime) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            format.setLenient(false);
            return format.parse(datetime);
        } catch (ParseException var2) {
            return null;
        }
    }


    public static int diffInMin(Date d1, Date d2) {
        long t1 = d1.getTime();
        long t2 = d2.getTime();
        double unit = 60000.0D;
        int absDiff = (int)Math.ceil((double)Math.abs(t1 - t2) / unit);
        return t1 > t2?absDiff:-absDiff;
    }

    public static int diffInSec(Date d1, Date d2) {
        long t1 = d1.getTime();
        long t2 = d2.getTime();
        double unit = 1000.0D;
        int absDiff = (int)Math.ceil((double)Math.abs(t1 - t2) / unit);
        return t1 > t2?absDiff:-absDiff;
    }

    public static boolean compareTime(String date1, String date2) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1= null;
        Date d2=null;
        try {
            d1 = sdf.parse(date1);
            d2= sdf.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long t1 = d1.getTime();
        long t2 = d2.getTime();
        return t1 > t2;
    }

    public static boolean compareNow(String date) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date d1= null;
        Date d2=new Date();
        try {
            d1 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long t1 = d1.getTime();
        long t2 = d2.getTime();
        return t1 > t2;
    }
    public static boolean compareTimeHour(String date1, String date2) {
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
        Date d1=null;
        Date d2=null;
        try {
            d1=sdf.parse(date1);
            d2= sdf.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long t1 = d1.getTime();
        long t2 = d2.getTime();
        return t1 > t2;
    }

    /**
     * date2比date1多的天数
     * @param start
     * @param end
     * @return
     */
    public static int differentDays(String start, String end) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date1=df.parse(start);
        Date date2=df.parse(end);
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
                    timeDistance += 366;
                }
                else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
            return day2-day1;
        }
    }
    public static List<Date[]> slice(Date beginDate, Date endDate, int interval_sec) {
        ArrayList pieces;
        Date nextEndDate;
        for(pieces = new ArrayList(); beginDate.compareTo(endDate) <= 0; beginDate = addSec(nextEndDate, 1)) {
            nextEndDate = addSec(beginDate, interval_sec);
            if(nextEndDate.after(endDate)) {
                nextEndDate = endDate;
            }

            Date[] piece = new Date[]{beginDate, nextEndDate};
            pieces.add(piece);
        }

        return pieces;
    }
}
